package com.gradle.sample;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class SpringHttpServlet extends HttpServlet {
    private transient WebApplicationContext webSpringContext;
    
    @Override
    public final void init() throws ServletException {
        super.init();
        webSpringContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
        //webSpringContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    }
    
    protected WebApplicationContext getWebSpringContext() {
        return webSpringContext;
    }
    
    protected <T> T getBeanInTheSpring (Class<T> clazz, String beanId) {
        return webSpringContext.getBean(beanId, clazz);
    }
}